/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcn.dal.hibernate;

import com.rcn.dal.interfaces.IBaseDAO;
import com.rcn.core.Patient;
import com.rcn.core.exception.RaccoonException;
import com.rcn.db.DBManager;
import com.rcn.hibernate.HibernateUtil;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * Delegate tasks to DBManager
 * Logs important info and errors but NEVER print an id.
 * Prints the stacktrace when an exception is catched.(TODO check this, should we print the stacktrace?)
 * @author Nicolas
 */ 
public class BaseDAO implements IBaseDAO{

	private static Logger log = LogManager.getLogger(BaseDAO.class.getName());
	
    public <T> long save(T item) {
        long generatedId = -1;
    	try{
    		log.debug("Saving entity of class: " + item.getClass().getName() + " ...");
    		generatedId = DBManager.getDBManager().save(item);
    		log.debug("Saved succesfully");
        }catch(HibernateException e){
        	log.error("Got an error trying to save entity.",e);
            throw new HibernateException(e.getMessage(),e.getCause());
        }
        return generatedId;
    }

    public <T> void update(T item) {
        try{
        	log.debug("Updating entity of class: " + item.getClass().getName() + " ...");
            DBManager.getDBManager().update(item);
            log.debug("Updated succesfully");
        }catch(HibernateException e){
        	log.error("Got an error trying to update entity.",e);
            throw new HibernateException(e.getMessage(), e.getCause());
        }
    }

    public <T> void saveOrUpdate(T item) {
        try{
        	log.debug("Saving or updating entity of class: " + item.getClass().getName() + " ...");
            DBManager.getDBManager().saveOrUpdate(item);
            log.debug("Saved or updated succesfully");
        }catch(HibernateException e){
        	log.error("Got an error trying to save or update entity.",e);
            throw new HibernateException(e.getMessage(), e.getCause());
        }
    }
    
    public <T> void saveOrUpdate(T item,Session s) {
        try{
        	log.debug("Saving or updating entity of class: " + item.getClass().getName() + " ...");
            DBManager.getDBManager().saveOrUpdate(item,s);
            log.debug("Saved or updated succesfully");
        }catch(HibernateException e){
        	log.error("Got an error trying to save or update entity.",e);
            throw new HibernateException(e.getMessage(), e.getCause());
        }
    }
    
    public Object getById(Class clazz, String id){
        Object obj;
    	try{
    		log.debug("Getting entity of class: " + clazz.getName() + " ...");
            obj =  DBManager.getDBManager().getData(clazz, id);
            log.debug("Got entity succesfully");
        }catch(HibernateException e){
        	log.error("Got an error trying to get the entity.",e);
            throw new HibernateException(e.getMessage(), e.getCause());
        }
    	return obj;
    }

    public List getAll(Class clazz){
    	List list = null;
        try{
        	log.debug("Getting all "+clazz.getName()+" ...");
        	list = DBManager.getDBManager().getAll(clazz.getName());
            log.debug("Got all succesfully.");
        }catch(HibernateException e){
        	log.error("Got an error trying to get all entities.",e);
            throw new HibernateException(e.getMessage(), e.getCause());
        }
        return list;
    }

    public <T> void delete(T item){
        try{
        	log.debug("Deleting entity of class " + item.getClass().getName() + " ...");
            DBManager.getDBManager().delete(item);
            log.debug("Deleted succesfully.");
        }catch(HibernateException e){
        	log.error("Got an error trying to delete the entity.",e);
            throw new HibernateException(e.getMessage(), e.getCause());
        }
    }
}
